﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Reflection;
using ExcelDna.Integration;
using FinPlusInterfaces;
using FinPlusAssembler;
using FinPlusCompQuant;
using s = FinPlusBaseStructure.FinPlusBaseStructure;
using d = FinPlusCompQuant.QLDateParser;

namespace FinPlusDNA
{
    public class FinPlusDNA
    {

        [ExcelFunction(Description = "assembly info", Category = "FinPlus.Assembly")]
        public static object FinPlusDNAAssembly()
        {
            return Assembly.GetExecutingAssembly().Info();
        }

        #region date
        
        [ExcelFunction(Description = "gets next biz date", Category = "FinPlus.Date")]
        public static object NextBizDate(DateTime Date, string Holidays)
        {
            try
            {
                var date = d.NextBizDate(Date, Holidays);
                return date;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "gets next biz date", Category = "FinPlus.Date")]
        public static object NextBizDate(DateTime Date, string Period, string BizDayConv, bool EndOfMonth, string Holidays)
        {
            try
            {
                var date = d.NextBizDate(Date, Period, BizDayConv, EndOfMonth, Holidays);
                return date;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "gets next imm date", Category = "FinPlus.Date")]
        public static object NextImmDate2(DateTime Date)
        {
            try
            {
                var date = d.NextImmDate(Date);
                return date;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        #region engine related

        [ExcelFunction(Description = "values trade", Category = "FinPlus.Analytics")]
        public static object MarketSet(string MarketName, DateTime TodaysDate)
        {
            try
            {
               var market = Markets.Instance.MarketSet(MarketName, TodaysDate);
               return MarketName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "values trade", Category = "FinPlus.Analytics")]
        public static object MarketValueTrade(string Market, string CacheName, string TradeId, string ControlString, string MakeVolatile)
        {
            try
            {
                var market = Markets.Instance.GetMarket(Market);
                var cache = Caches.Instance.GetCache(CacheName);
                var marketValueTrade = market.MarketValueTrade(cache, TradeId, ControlString.Split(','));

                return marketValueTrade;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //[ExcelFunction(Description = "values trades", Category = "FinPlus.Analytics")]
        //public static object MarketValueTrades(string Market, string CacheName, string ControlString, string MakeVolatile)
        //{
        //    try
        //    {
        //        //return ql.MarketValueTrades(Market, CacheName, ControlString);
        //    }
        //    catch (Exception e)
        //    {
        //        return e.ToString();
        //    }
        //}
        
        #endregion

        #region pricing structures

        [ExcelFunction(Description = "builds indexs", Category = "FinPlus.Analytics")]
        public static object SetIndex(string MarketName, string CurveName, string IndexName, string IndexType, string Tenor, string Fixings)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var index = new Index(market, CurveName, IndexName, IndexType, Tenor, Fixings);
                return index.Name;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds yield curve", Category = "FinPlus.Analytics")]
        public static object YieldCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, string Rates, string DayCount, double Tolerance, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var yieldCurve = new YieldCurve(market, CurveName, DiscountCurveName, SettlementDate, Rates.Split(','), DayCount, Holidays);
                return CurveName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds default curve", Category = "FinPlus.Analytics")]
        public static object DefaultCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, string Spreads, double RecoveryRate, string DayCount, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var defaultCureCurve = new DefaultCurve(market, CurveName, DiscountCurveName, SettlementDate, Spreads.Split(','), RecoveryRate, DayCount, Holidays);
                return CurveName;//defaultCureCurve.CurveName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds vol surf", Category = "FinPlus.Analytics")]
        public static object VolSurf(string MarketName, string CurveName, string VolSurfName, string Vols, string Index, string Model)
        {
            try
            {
                var volSurf = new VolSurf(MarketName, CurveName, VolSurfName, Vols, Index, Model);
                return volSurf.Name;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds flat forward yield curve", Category = "FinPlus.Analytics")]
        public static object FlatForwardCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, int Rate, string DayCount)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName); 
                var flatForward = new FlatForwardCurve(market, CurveName, DiscountCurveName, SettlementDate, Rate, DayCount);
                return CurveName; 
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds flat forward yield curve", Category = "FinPlus.Analytics")]
        public static object FlatForwardCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, int Rate, string DayCount, string CpnFrq, string Compounded)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var flatForward = new FlatForwardCurve(market, CurveName, DiscountCurveName, SettlementDate, Rate, DayCount, CpnFrq, Compounded);
                return CurveName; 
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds bond curve", Category = "FinPlus.Analytics")]
        public static object BondCurve(string MarketName, string CurveName, string DiscountCurveName, DateTime SettlementDate, string Rates, string DayCount, double Tolerance, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var bondCurve = new BondCurve(market, CurveName, DiscountCurveName, SettlementDate, Rates.Split(','), DayCount, Tolerance, Holidays);
                return CurveName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds depo rate", Category = "FinPlus.Analytics")]
        public static object DepoRate(string MarketName, string RateName, double Rate, string Tenor, int FixingDays, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var depoRate = new DepoRate(market, RateName, Rate, Tenor, FixingDays, DayCount, BizConv, Holidays, true);
                return RateName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fra rate", Category = "FinPlus.Analytics")]
        public static object FraRate(string MarketName, string RateName, double Rate, int Start, int End, int FixingDays, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var fraRate = new FraRate(market, RateName, Rate, Start, End, FixingDays, DayCount, BizConv, Holidays);
                return RateName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds future rate", Category = "FinPlus.Analytics")]
        public static object FutRate(string MarketName, string RateName, double Rate, DateTime StartDate, int FutMonths, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var futRate = new FutRate(market, RateName, Rate, StartDate, FutMonths, DayCount, BizConv, Holidays);

                return RateName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds swap rate", Category = "FinPlus.Analytics")]
        public static object SwapRate(string MarketName, string RateName, double Rate, double Spread, string Tenor, int FixingDays, int ForwardStart, string FloatLegIndex, string FixedLegFreq, string FixedLegDayCount, string FixedLegBizConv, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var swapRate = new SwapRate(market, RateName, Rate, Spread, Tenor, FixingDays, ForwardStart, FloatLegIndex, FixedLegFreq, FixedLegDayCount, FixedLegBizConv, Holidays);
                return RateName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds cds spread", Category = "FinPlus.Analytics")]
        public static object CdsSpread(string MarketName, string RateName, double Spread, double RecoveryRate, string Tenor, int FixingDays, string DicountCurveName, string Freq, string DayCount, string BizConv, string Holidays)
        {
            try
            {
                var market = Markets.Instance.GetMarket(MarketName);
                var cdsSpread = new CdsSpread(market, RateName, Spread, RecoveryRate, Tenor, FixingDays, DicountCurveName, Freq, DayCount, BizConv, Holidays);
                return RateName; 
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fixed bond", Category = "FinPlus.Analytics")]
        public static object FixedRateBondBuild(string MarketName, string Instrument, string CurveName, string ISIN, string SEDOL, string CUSIP, string BBIdent, double Coupon, double MktPrice, string CouponFrq, double Redemption, int SettlementDays, DateTime EffectiveDate, DateTime TerminationDate, string DayCount, string PayConv, string TermConv, string BondConv, string Holidays)
        {
            try
            {
                var fixedRateBondBuild = new FixedRateBondBuild(MarketName, Instrument, Coupon, MktPrice, CouponFrq, Redemption, SettlementDays, EffectiveDate, TerminationDate, DayCount, PayConv, TermConv, BondConv, Holidays);
                //not used by ql proxy but stored for quering/reference via book ISIN, SEDOL, CUSIP and BBIdent,
                return Instrument;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds floating bond", Category = "FinPlus.Analytics")]
        public static object FloatingRateBondBuild(string MarketName, string Instrument, string CurveName, string ISIN, string SEDOL, string CUSIP, string BBIdentity, double Spread, string Curve, string Index, string CouponFrq, double Redemption, int SettlementDays, DateTime EffectiveDate, DateTime TerminationDate, string DayCount, string PayConv, string TermConv, string BondConv, string Holidays)
        {
            try
            {
                //not used by ql proxy but stored for quering/reference via book ISIN, SEDOL, CUSIP and BBIdent,
                var floatingRateBondBuild = new FloatingRateBondBuild(MarketName, Instrument, Spread, CurveName, Index, CouponFrq, Redemption, SettlementDays, EffectiveDate, TerminationDate, DayCount, PayConv, TermConv, BondConv, Holidays);
                
                return Instrument;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds zero coupon bond", Category = "FinPlus.Analytics")]
        public static object ZeroCouponBondBuild(string MarketName, string Instrument, string CurveName, string ISIN, string SEDOL, string CUSIP, string BBIdentity, double Redemption, int SettlementDays, DateTime EffectiveDate, DateTime TerminationDate, string BondConv, string Holidays)
        {
            try
            {
                var zeroCouponBondBuild = new ZeroCouponBondBuild(MarketName, Instrument, Redemption, SettlementDays, EffectiveDate, TerminationDate, BondConv, Holidays);
                
                return Instrument;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        #region instruments
        //rate
        [ExcelFunction(Description = "builds vanilla swap", Category = "FinPlus.Analytics")]
        public static object VanillaSwap(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double Nominal, DateTime Start, DateTime Maturity, double FixedRate, string PayRec, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays)
        {
            //same used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var vanillaSwap =  new VanillaSwapTrade(MarketName, CacheName, TempId, CurveName, Nominal, Start, Maturity, FixedRate, PayRec, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays);
                return vanillaSwap.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds basis swap", Category = "FinPlus.Analytics")]
        public static object BasisSwap(string Book, string Counterparty, string MarketName, string CacheName, string TempId, double Nominal, DateTime Start, DateTime Maturity, string PayRec, string CurveName, string Index, double Spread, string FltLegFrq, string FltLegConv, string FltLegDayCount, string CurveName2, string Index2, double Spread2, string FltLegFrq2, string FltLegConv2, string FltLegDayCount2, string Holidays)
        {
            //same used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var basisSwap = new BasisSwapTrade(MarketName, CacheName, TempId, Nominal, Start, Maturity, PayRec,
                    CurveName, Index, Spread, FltLegFrq, FltLegConv, FltLegDayCount,
                    CurveName2, Index2, Spread2, FltLegFrq2, FltLegConv2, FltLegDayCount2,
                    Holidays);

                return basisSwap.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds floating leg", Category = "FinPlus.Analytics")]
        public static object FloatingLeg(string Book, string Counterparty, string MarketName, string CacheName, string TempId, double Nominal, DateTime Start, DateTime Maturity, string PayRec, string CurveName, string Index, double Spread, string FltLegFrq, string FltLegConv, string FltLegDayCount, string Holidays)
        {
            //same used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var floatinLeg = new FloatingLegTrade(MarketName, CacheName, TempId, Nominal, Start, Maturity, PayRec,
                    CurveName, Index, Spread, FltLegFrq, FltLegConv, FltLegDayCount,
                    Holidays);
                return floatinLeg.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fixed leg", Category = "FinPlus.Analytics")]
        public static object FixedLeg(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double Nominal, DateTime Start, DateTime Maturity, double FixedRate, string PayRec, string Index, string FixLegFrq, string FixLegConv, string FixLegDayCount, string Holidays)
        {
            //same used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var fixedLeg = new FixedLegTrade(MarketName, CacheName, TempId, CurveName, Nominal, Start, Maturity, FixedRate, PayRec, Index, FixLegFrq, FixLegConv, FixLegDayCount, Holidays);
                return fixedLeg.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds fra", Category = "FinPlus.Analytics")]
        public static object ForwardRateAgreement(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double Nominal, DateTime Start, DateTime Maturity, double FixedRate, string PayRec, string Index)
        {
            //same used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var fra =  new ForwardRateAgreementTrade(MarketName, CacheName, TempId, CurveName, Nominal, Start, Maturity, FixedRate, PayRec, Index);
                return fra.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //rate options
        [ExcelFunction(Description = "builds swaption", Category = "FinPlus.Analytics")]
        public static object Swaption(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, string VolSurfName, double Nominal, DateTime Start, DateTime Maturity, double Strike, string PayRec, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays)
        {
            //used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var swaption = new SwaptionTrade(MarketName, CacheName, TempId, CurveName, VolSurfName, Nominal, Start, Maturity, Strike, PayRec, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays);
                return swaption.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds bermudanswaption", Category = "FinPlus.Analytics")]
        public static object BermudanSwaption(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, string VolSurfName, double Nominal, DateTime Start, DateTime Maturity, double Strike, string PayRec, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays)
        {
            //used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var bermudanSwaption =  new BermudanSwaptionTrade(MarketName, CacheName, TempId, CurveName, VolSurfName, Nominal, Start, Maturity, Strike, PayRec, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays);
                return bermudanSwaption.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //credit
        [ExcelFunction(Description = "builds cds", Category = "FinPlus.Analytics")]
        public static object CreditDefaultSwap(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string CurveName, double QuotedSpread, string ProtSellBuy, double Nominal, DateTime Start, DateTime Maturity, string PremFreq, string PremConv, string PayConv, string DayCount, string Holidays)
        {
            //used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var creditDefaultSwap = new CreditDefaultSwapTrade(MarketName, CacheName, TempId, CurveName, QuotedSpread, ProtSellBuy, Nominal, Start, Maturity, PremFreq, PremConv, PayConv, DayCount, Holidays);
                return creditDefaultSwap.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        //bond
        [ExcelFunction(Description = "builds fixed rate bond", Category = "FinPlus.Analytics")]
        public static object FixedRateBond(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string CurveName, double Nominal, DateTime Settlement, double Price)
        {
            //used for booking so TimeStamp book and counterparty FinPlusBooker will parse and save to db
            try
            {
                var fixedRateBond = new FixedRateBond(MarketName, CacheName, TempId, BondName, CurveName, Nominal, Settlement, Price);
                return BondName;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds floating rate bond", Category = "FinPlus.Analytics")]
        public static object FloatingRateBond(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string CurveName, double Nominal, DateTime Settlement, double Price)
        {
            try
            {
                var floatingRateBond  = new FloatingRateBond(MarketName, CacheName, TempId, BondName, CurveName, Nominal, Settlement, Price);
                return floatingRateBond.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "builds zero coupon bond", Category = "FinPlus.Analytics")]
        public static object ZeroCouponBond(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string CurveName, double Nominal, DateTime Settlement, double Price)
        {
            try
            {
                var zeroCouponBond = new ZeroCouponBond(MarketName, CacheName, TempId, BondName, CurveName, Nominal, Settlement, Price);
                return zeroCouponBond.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "repo fixed bond", Category = "FinPlus.Analytics")]
        public static object Repo(string Book, string Counterparty, string MarketName, string CacheName, string TempId, string BondName, string RepoCurveName, string BondCurveName, double Strike, double Nominal, DateTime SettlementDate, DateTime DeliveryDate, int SettlementDays, string RepoConv, string BondConv, string Holidays)
        {
            try
            {
                var repo = new RepoTrade(MarketName, CacheName, TempId, BondName, RepoCurveName, BondCurveName, Strike, Nominal, SettlementDate, DeliveryDate, SettlementDays, RepoConv, BondConv, Holidays);
                return repo.Id;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        //info
        #region info

        [ExcelFunction(Description = "gets pod list", Category = "FinPlus.Info")]
        public static object PodList(string function)
        {
            try
            {
                var list = new List<string>();
                MethodInfo[] mi = typeof(FinPlusDNA).GetMethods(BindingFlags.Public | BindingFlags.Static);
                foreach (var m in mi) list.Add(m.Name);
                list.Sort();
                return list.ToArray().Transpose();
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "gets info", Category = "FinPlus.Info")]
        public static object PodFields(string function)
        {
            try
            {
                var list = new List<string>();

                //add core 
                list.Add(PodField.Id.ToString());
                list.Add(PodField.Type.ToString());
                list.Add(PodField.TimeStamp.ToString());
                var method = typeof(FinPlusDNA).GetMethod(function);
                var parms = method.GetParameters();
                foreach (var parm in parms) list.Add(parm.Name);

                return list.ToArray().Transpose();

            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        #endregion

        //finplus untility wrappers
        #region FinPlusBaseUtility
        [ExcelFunction(Description = "gets string from array 2d", Category = "FinPlus.Parsing")]
        public static string StringFromArray2d(object[,] array, string delimiterRow, string delimiterCol)
        {
            try
            {
                return array.StringFrom2d(delimiterRow == "" ? ';' : char.Parse(delimiterRow), delimiterCol == "" ? ',' : char.Parse(delimiterCol));
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "converts string to array 2d", Category = "FinPlus.Parsing")]
        public static object StringToArray2d(string val, string delimiterRow, string delimiterCol)
        {
            try
            {
                return val.ToArray2d(delimiterRow == "" ? ';' : char.Parse(delimiterRow), delimiterCol == "" ? ',' : char.Parse(delimiterCol));
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        [ExcelFunction(Description = "converts string with info to object", Category = "FinPlus.Parsing")]
        public static object StringWithInfoToObject(string str, string infoType)
        {
            try
            {
                return StringWithInfoToObject(str, infoType == string.Empty ? "String" : infoType);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        public static object StringWithInfoToObject(string value, string InfoType, char delimiterInfo = '|', char delimiterRow = '%', char delimiterCol = '$')
        {
            var s = value.Split(delimiterInfo);
            var val = s[s.GetUpperBound(0)];

            switch (InfoType)
            {
                case "String": return val;
                case "Number": return double.Parse(val);
                case "Array1D": return val.ToArray1d();
                case "Array2D": return val.ToArray2d();
            }
            return "#info type not recognised";
        }
 

        #endregion

        //finplus struture wrappers
        #region FinPlusBaseStructure
        
        //permissions
        [ExcelFunction(Description = "FinPlusBaseStructure Permission", Category = "FinPlus.Permission")]
        public static object Permission(string User, string Name, string BeanName, string BeanValue, bool CanRead, bool CanWrite) { s.Permission(User, Name, BeanName, BeanValue, CanRead, CanWrite); return User; }

        //adapters
        [ExcelFunction(Description = "FinPlusBaseStructure QuoteAdapter", Category = "FinPlus.Adapter")]
        public static object QuoteAdapter(string QuoteSource, IPod ReceiveAction, IPod PublishAction) { s.QuoteAdapter(QuoteSource, ReceiveAction, PublishAction); return QuoteSource; }

        [ExcelFunction(Description = "FinPlusBaseStructure TradeAdapter", Category = "FinPlus.Adapter")]
        public static object TradeAdapter(string InstrumentType, IPod ReceiveQuoteAction, IPod ParseTradeAction, IPod ResponseAction) { s.TradeAdapter(InstrumentType, ReceiveQuoteAction, ParseTradeAction, ResponseAction); return InstrumentType; }

        [ExcelFunction(Description = "FinPlusBaseStructure Index", Category = "FinPlus.MarketLevel")]
        public static object Index(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Index(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "FinPlusBaseStructure Price", Category = "FinPlus.MarketLevel")]
        public static object Price(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Price(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "FinPlusBaseStructure Rate", Category = "FinPlus.MarketLevel")]
        public static object Rate(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Rate(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "FinPlusBaseStructure Spread", Category = "FinPlus.MarketLevel")]
        public static object Spread(string Market, string Instrument, string BuySell, double Level, string Source, int Date) { s.Spread(Market, Instrument, BuySell, Level, Source, Date); return Level; }

        [ExcelFunction(Description = "FinPlusBaseStructure Setting", Category = "FinPlus.MarketLevel")]
        public static object Setting(string IndexName, double Level, int Date) { s.Setting(IndexName, Level, Date); return IndexName; }

        //market making
        [ExcelFunction(Description = "FinPlusBaseStructure Quote", Category = "FinPlus.Structure")]
        public static object Quote(string QuoteRef, string Instrument, string BuySell, double Quote, double Size, string QuoteStyle, string QuoteType, string RateType, string QuoteSource, string Counterparty) { s.Quote(QuoteRef, Instrument, BuySell, Quote, Size, QuoteStyle, QuoteType, RateType, QuoteSource, Counterparty); return QuoteRef; }

        //market making structure
        [ExcelFunction(Description = "FinPlusBaseStructure QuoteStyle", Category = "FinPlus.Structure")]
        public static object QuoteStyle(string QuoteStyle) { s.QuoteStyle(QuoteStyle); return QuoteStyle; }

        [ExcelFunction(Description = "FinPlusBaseStructure QuoteType", Category = "FinPlus.Structure")]
        public static object QuoteType(string QuoteType) { s.QuoteType(QuoteType); return QuoteType; }

        [ExcelFunction(Description = "FinPlusBaseStructure QuoteSource", Category = "FinPlus.Structure")]
        public static object QuoteSource(string QuoteSource, string User, string Location) { s.QuoteSource(QuoteSource, User, Location); return QuoteSource; }

        [ExcelFunction(Description = "FinPlusBaseStructure QuoteInterets", Category = "FinPlus.Structure")]
        public static object QuoteInterest(string Instrument, string Book, bool IsAxe, string BuySell, double Quote, double Size, string QuoteStyle, string QuoteType, string QuoteMark, string RateType, string Trader) { s.QuoteInterest(Instrument, Book, IsAxe, BuySell, Quote, Size, QuoteStyle, QuoteType, QuoteMark, RateType, Trader); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure Instrument", Category = "FinPlus.Structure")]
        public static object Instrument(string Instrument, string Ccy, string LinkedCurve, string InstrumentName, string InstrumentType, string RateType, double ChgMin, double SizeMin, double SizeMax, double ShockUnit) { s.Instrument(Instrument, Ccy, LinkedCurve, InstrumentName, InstrumentType, RateType, ChgMin, SizeMin, SizeMax, ShockUnit); return Instrument; }      

        [ExcelFunction(Description = "FinPlusBaseStructure InstrumentSpread", Category = "FinPlus.Structure")]
        public static object InstrumentSpread(string Instrument, string SpreadName, string Spread, string SpreadType, bool IsMktToMkt, double BuyAdj, double SellAdj, double Weight, string Trader) { s.InstrumentSpread(Instrument, SpreadName, Spread, SpreadType, IsMktToMkt, BuyAdj, SellAdj, Weight, Trader); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure InstrumentType", Category = "FinPlus.Structure")]
        public static object InstrumentType(string InstrumentType) { s.InstrumentType(InstrumentType); return InstrumentType; }

        [ExcelFunction(Description = "FinPlusBaseStructure RateType", Category = "FinPlus.Structure")]
        public static object RateType(string RateType) { s.InstrumentType(RateType); return RateType; }

        [ExcelFunction(Description = "FinPlusBaseStructure QuoteMark", Category = "FinPlus.Structure")]
        public static object QuoteMark(string QuoteMark) { s.InstrumentType(QuoteMark); return QuoteMark; }

        [ExcelFunction(Description = "FinPlusBaseStructure Ccy", Category = "FinPlus.Structure")]
        public static object Ccy(string Ccy, string Name) { s.Ccy(Ccy, Name); return Ccy; }

        [ExcelFunction(Description = "FinPlusBaseStructure BuySell", Category = "FinPlus.Structure")]
        public static object BuySell(string BuySell) { s.BuySell(BuySell); return BuySell; }

        [ExcelFunction(Description = "FinPlusBaseStructure SpreadType", Category = "FinPlus.Structure")]
        public static object SpreadType(string SpreadType) { s.SpreadType(SpreadType); return SpreadType; }

        [ExcelFunction(Description = "FinPlusBaseStructure BizConv", Category = "FinPlus.Structure")]
        public static object BizConv(string BizConv) { s.BizConv(BizConv); return BizConv; }

        [ExcelFunction(Description = "FinPlusBaseStructure DayCount", Category = "FinPlus.Structure")]
        public static object DayCount(string DayCount) { s.DayCount(DayCount); return DayCount; }

        [ExcelFunction(Description = "FinPlusBaseStructure Holidays", Category = "FinPlus.Structure")]
        public static object Holidays(string Holidays) { s.Holidays(Holidays); return Holidays; }

        [ExcelFunction(Description = "FinPlusBaseStructure Frequency", Category = "FinPlus.Structure")]
        public static object Freq(string Freq) { s.Freq(Freq); return Freq; }

        [ExcelFunction(Description = "FinPlusBaseStructure PayRec", Category = "FinPlus.Structure")]
        public static object PayRec(string PayRec) { s.PayRec(PayRec); return PayRec; }

        [ExcelFunction(Description = "FinPlusBaseStructure CurveName", Category = "FinPlus.Structure")]
        public static object CurveName(string CurveName, string CurveType) { s.CurveName(CurveName, CurveType); return CurveName; }

        [ExcelFunction(Description = "FinPlusBaseStructure CurveName", Category = "FinPlus.Structure")]
        public static object CurveType(string CurveType) { s.CurveType(CurveType); return CurveType; }

        //OTC Instrument templates
        [ExcelFunction(Description = "FinPlusBaseStructure VanillaSwapTemplate", Category = "FinPlus.Template")]
        public static object VanillaSwapBuild(string Instrument, string CurveName, int SettlementDays, bool EndOfMonth, string Maturity, string Index, double Spread, string FixLegFrq, string FltLegFrq, string FixLegConv, string FltLegConv, string FixLegDayCount, string FltLegDayCount, string Holidays) { s.VanillaSwapBuild(Instrument, CurveName, SettlementDays, EndOfMonth, Maturity, Index, Spread, FixLegFrq, FltLegFrq, FixLegConv, FltLegConv, FixLegDayCount, FltLegDayCount, Holidays); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure ForwardRateAgreementBuild", Category = "FinPlus.Template")]
        public static object ForwardRateAgreementBuild(string Instrument, string CurveName, int SettlementDays, string Start, string Maturity, string Index, string DayCount, string BizConv, string Holidays) { s.ForwardRateAgreementBuild(Instrument, CurveName, SettlementDays, Start, Maturity, Index, DayCount, BizConv, Holidays); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure DepositLoanBuild", Category = "FinPlus.Template")]
        public static object DepositLoanBuild(string Instrument, string CurveName, int SettlementDays, string Maturity, string Index, string DayCount, string BizConv, string Holidays) { s.DepositLoanBuild(Instrument, CurveName, SettlementDays, Maturity, Index, DayCount, BizConv, Holidays); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure ZCMBuild", Category = "FinPlus.Template")]
        public static object ZCMBuild(string Instrument, string Maturity, int SettlementDays, string DayCount, string BizConv, string Holidays) { s.ZCMBuild(Instrument, Maturity, SettlementDays, DayCount, BizConv, Holidays); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure IRFutureBuild", Category = "FinPlus.Template")]
        public static object IRFutureBuild(string Instrument, string CurveName, int StartDate, int FutMonths, string DayCount, string BizConv, string Holidays){s.IRFutureBuild(Instrument, CurveName, StartDate, FutMonths, DayCount, BizConv, Holidays); return Instrument; }
 
        [ExcelFunction(Description = "FinPlusBaseStructure CreditDefaultSwap", Category = "FinPlus.Template")]
        public static object CreditDefaultSwapBuild(string Instrument, string CurveName, int SettlementDays, string Maturity, string PremFreq, string PremConv, string PayConv, string DayCount, string Holidays) { s.CreditDefaultSwapBuild(Instrument, CurveName, SettlementDays, Maturity, PremFreq, PremConv, PayConv, DayCount, Holidays); return Instrument; }

        [ExcelFunction(Description = "FinPlusBaseStructure YieldCurveBuild", Category = "FinPlus.Template")]
        public static object YieldCurveBuild(string CurveName, string Ccy, int NumDepo, int NumFut, int NumFra, int NumSwap, string Index, string DayCount, string Tolerance, string Holidays) { s.YieldCurveBuild(CurveName, Ccy, NumDepo, NumFut, NumFra, NumSwap, Index, DayCount, Tolerance, Holidays); return CurveName; }

        [ExcelFunction(Description = "FinPlusBaseStructure DefaultCurveBuild", Category = "FinPlus.Template")]
        public static object DefaultCurveBuild(string CurveName, string Ccy, int NumSpread, string CreditName, double RecoveryRate, string DayCount, string Holidays) { s.DefaultCurveBuild(CurveName, Ccy, NumSpread, CreditName, RecoveryRate, DayCount, Holidays); return CurveName; }

        [ExcelFunction(Description = "FinPlusBaseStructure BondCurveBuild", Category = "FinPlus.Template")]
        public static object BondCurveBuild(string CurveName, string Ccy, int NumZCM, int NumBond, string DayCount, double Tolerance, string Holidays) { s.BondCurveBuild(CurveName, Ccy, NumZCM, NumBond, DayCount, Tolerance, Holidays); return CurveName; }

        [ExcelFunction(Description = "FinPlusBaseStructure IndexBuild", Category = "FinPlus.Template")]
        public static object IndexBuild(string IndexName, string CurveName, string IndexType, string Tenor) { s.IndexBuild(IndexName, CurveName, IndexType, Tenor); return IndexName; }

        //business structure
        [ExcelFunction(Description = "FinPlusBaseStructure Location", Category = "FinPlus.Structure")]
        public static object Location(string Location, string Country, string Region) { s.Location(Location, Country, Region); return Location; }

        [ExcelFunction(Description = "FinPlusBaseStructure Department", Category = "FinPlus.Structure")]
        public static object Department(string Department, string Location) { s.Department(Department, Location); return Department; }

        [ExcelFunction(Description = "FinPlusBaseStructure Desk", Category = "FinPlus.Structure")]
        public static object Desk(string Desk, string Department) { s.Desk(Desk, Department); return Desk; }

        [ExcelFunction(Description = "FinPlusBaseStructure Book", Category = "FinPlus.Structure")]
        public static object Book(string Book, string Desk, string Department, string Location) { s.Book(Book, Desk, Department, Location); return Book; }

        [ExcelFunction(Description = "FinPlusBaseStructure User", Category = "FinPlus.Admin")]
        public static object User(string User, string UserName, string Desk, string Location) { s.User(User, UserName, Desk, Location); return User; }

        //country structure
        [ExcelFunction(Description = "FinPlusBaseStructure Region", Category = "FinPlus.Admin")]
        public static object Region(string Region) { s.Region(Region); return Region; }

        [ExcelFunction(Description = "FinPlusBaseStructure Country", Category = "FinPlus.Admin")]
        public static object Country(string Country, string Region) { s.Country(Country, Region); return Country; }

        //company structure
        [ExcelFunction(Description = "FinPlusBaseStructure Counterparty", Category = "FinPlus.Structure")]
        public static object Counterparty(string Counterparty, string LongName, string Identifier, string DefaultCollateralCurve, string Country, string Sector, string Region, string InternalRating, string MoodysRating, string SnPRating){ s.Counterparty(Counterparty, LongName, Identifier, DefaultCollateralCurve, Country, Sector, Region, InternalRating, MoodysRating, SnPRating); return Counterparty; }

        [ExcelFunction(Description = "FinPlusBaseStructure CounterpartyQuoteTier", Category = "FinPlus.Structure")]
        public static object CounterpartyQuoteTier(string Counterparty, string InstrumentType, double Adjustment) { s.CounterpartyQuoteTier(Counterparty, InstrumentType, Adjustment); return Counterparty; }

        [ExcelFunction(Description = "FinPlusBaseStructure Sector", Category = "FinPlus.Admin")]
        public static object Sector(string Sector) { s.Sector(Sector); return Sector; }

        [ExcelFunction(Description = "FinPlusBaseStructure InternalRating", Category = "FinPlus.Admin")]
        public static object InternalRating(string InternalRating, int Index) { s.InternalRating(InternalRating, Index); return InternalRating; }

        [ExcelFunction(Description = "FinPlusBaseStructure MoodysRating", Category = "FinPlus.Admin")]
        public static object MoodysRating(string MoodysRating, int Index) { s.MoodysRating(MoodysRating, Index); return MoodysRating; }

        [ExcelFunction(Description = "FinPlusBaseStructure SnPRating", Category = "FinPlus.Admin")]
        public static object SnPRating(string SnPRating, int Index) { s.SnPRating(SnPRating, Index); return SnPRating; }

        //admin
        [ExcelFunction(Description = "FinPlusBaseStructure application info", Category = "FinPlus.Admin")]
        public static object Application(string Application, string Description, string Owner) { s.Application(Application, Description, Owner); return Application; }

        [ExcelFunction(Description = "sets to be mapped and creates tables if they do not exist", Category = "FinPlus.StaticData")]
        public static object PodMap(string Group, string Name) { s.PodMap(Group, Name); return Group; }
        #endregion

    }
}
